package com.activity.shop;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;

import com.activity.adapter.AttanceMemberAdapter;
import com.activity.bean.EmployerBean;
import com.api.param.StoreParam;
import com.api.viewModel.StoreVM;
import com.donkingliang.groupedadapter.adapter.GroupedRecyclerViewAdapter;
import com.donkingliang.groupedadapter.holder.BaseViewHolder;
import com.example.apple.ddhr_new.R;
import com.example.apple.ddhr_new.activity.setting.BaseListViewActivity;
import com.example.apple.ddhr_new.utils.ToastUtil;
import com.google.gson.Gson;

import java.util.ArrayList;

public class AttanceMemberActivity extends BaseListViewActivity<StoreVM> {
    private StoreParam storeParam=new StoreParam();
    private   AttanceMemberAdapter attanceMemberAdapter ;
    private ArrayList<EmployerBean>employerBeans = new ArrayList<>();
    @Override
    protected int titleText() {
        return R.string.kaoqin_renyuan;
    }

    @Override
    protected void created(@Nullable Bundle savedInstanceState) {
        super.created(savedInstanceState);


        Gson gson =new Gson();
        ArrayList arrayList = gson.fromJson(getIntent().getExtras().getString("param"),ArrayList.class);


        for (int i =0;i<arrayList.size();i++){
            EmployerBean employerBean = gson.fromJson((gson.toJson(arrayList.get(i))),EmployerBean.class);
            employerBeans.add(employerBean);

        }
        setTitleRight("完成");
        refreshLayout.setEnableLoadMore(false);
        attanceMemberAdapter = new AttanceMemberAdapter(this);
        recycleView.setAdapter(attanceMemberAdapter);


        listViewModel.members.observe(this,res->{
            if(res!=null){


                for (int i=0;i<employerBeans.size();i++){
                    for(int j=0;j<res.size();j++){
                        if(res.get(j).getUserid().equals(employerBeans.get(i).getUserid())){
                            res.get(j).selected  =employerBeans.get(i).selected;
                            break;
                        }
                    }
                }
                EmployerBean bean = new EmployerBean();
                bean.setUsernickname("全选");
                bean.setUserid("-1000");
                boolean selectAll = false;

                for (int i=0;i<attanceMemberAdapter.getData().size();i++){

                    if(attanceMemberAdapter.getData().get(i).getUserid().equals("-1000")){
                        selectAll=attanceMemberAdapter.getData().get(i).selected;
                        break;
                    }
                }

                bean.selected =selectAll;
                res.add(0,bean);


                attanceMemberAdapter.getData().clear();
                attanceMemberAdapter.getData().addAll(res);
                attanceMemberAdapter.notifyDataChanged();
            }
            headerFreshEnd();
        });

        attanceMemberAdapter.setOnChildClickListener(new GroupedRecyclerViewAdapter.OnChildClickListener() {
            @Override
            public void onChildClick(GroupedRecyclerViewAdapter adapter, BaseViewHolder holder, int groupPosition, int childPosition) {

                EmployerBean bean = attanceMemberAdapter.getData().get(childPosition);


                if(bean.getUserid().equals("-1000")){

                    bean.selected = !bean.selected;
                    for (int i=0;i<attanceMemberAdapter.getData().size();i++)
                    {
                        attanceMemberAdapter.getData().get(i).selected = bean.selected;
                    }

                    employerBeans.clear();

                    if(bean.selected){
                        employerBeans.addAll(attanceMemberAdapter.getData());
                        employerBeans.remove(0);
                    }
                }else {

                    bean.selected = !bean.selected;
                    if(bean.selected){
                        employerBeans.add(bean);
                    }else
                    {
                        int index = -1;
                        for (int i =0;i<employerBeans.size();i++){

                            if(employerBeans.get(i).getUserid().equals(bean.getUserid())){

                                index =i;
                                break;
                            }
                        }

                        if(index>-1){
                            employerBeans.remove(index);
                        }

                    }
                }
                attanceMemberAdapter.notifyDataChanged();

            }
        });
        headerFreshing();
    }

    @Override
    protected void headerFreshing() {

        addSubscription(listViewModel.getMember(storeParam ));
    }

    @Override
    protected void titleRightClick() {
        if(employerBeans.size()>0){

            Intent intent = new Intent();

            Gson gson = new Gson();

            intent.putExtra("data",gson.toJson(employerBeans));
            setResult(100,intent);

            finish();
        }else {
            ToastUtil.toast(this,"请选择考勤人员");
        }
    }
}
